package com.zhxh.nzpos.goods;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.ListView;

import com.zhxh.nzpos.BaseActivity;
import com.zhxh.nzpos.R;
import com.zhxh.nzpos.dao.SuppliersDao;
import com.zhxh.nzpos.model.Suppliers;
import com.zhxh.nzpos.sell.ItemDeleteCallback;

/**
 * 供货商管理
 * 
 * @author wangzt
 *
 */
public class GoodsSuppliersActivity extends BaseActivity implements ItemDeleteCallback {

	private ArrayList<Suppliers> suppliersItems;
	private SupplierListAdapter listAdapter;
	SuppliersDao suppliersDao;
	
	private int position;
	
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_goods_supplier );
        suppliersDao = new SuppliersDao(this);
        initViews();
        
        
    }
	
	private void initViews() {
		ListView listView = (ListView) findViewById(R.id.goods_sullpier_listview);
		//suppliersItems = new ArrayList<Suppliers>();
		initData();
		// TODO 扫描条码添加一项
		//final EditText dealOcr = (EditText) findViewById(R.id.goods_ocr_et);
		ImageView addBtn = (ImageView) findViewById(R.id.goods_add_btn);
		addBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
			//	Editable ocr = dealOcr.getText();
			//	if (ocr!= null) {
			//		String ocrStr = ocr.toString();
					Intent intent = new Intent();
					intent.setClass(getApplicationContext(), SuppliersAddActivity.class);
					//startActivity(intent);
					startActivityForResult(intent, 0);
			//	}
			}
		});
		
		listAdapter = new SupplierListAdapter(getLayoutInflater(), suppliersItems, this);
		listView.setAdapter(listAdapter);
		
/*		ImageView settleBtn = (ImageView) findViewById(R.id.goods_ok_btn);
		settleBtn.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				// TODO计算并传递参数到SettleAccountActivity
				//Intent intent = new Intent();
				//intent.setClass(getApplicationContext(), SettleAccountActivity.class);
				//startActivity(intent);
				//finish();
				//ViewUtil.showProgressDialog(GoodsSuppliersActivity.this, "Loading");
				//ViewUtil.cancelProgressDialog();
				Intent intent = new Intent();
				intent.setClass(getApplicationContext(), SuppliersAddActivity.class);
				//startActivity(intent);
				startActivityForResult(intent, 0);
				PopupWindow popup = makePopupWindow(GoodsSuppliersActivity.this);
				
				popup.setFocusable(true); //设置PopupWindow可获得焦点
				popup.setTouchable(true); //设置PopupWindow可触摸
				popup.setOutsideTouchable(true); //设置非PopupWindow区域可触摸
				
				popup.showAtLocation(v.getRootView(),Gravity.RIGHT | Gravity.BOTTOM, 10,10);
				
				
			}
		});*/
		ImageView cancelBtn = (ImageView) findViewById(R.id.goods_cancel_btn);
		cancelBtn.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				finish();
			}
		});

	}

	public void onDelete(int pos) {

		position = pos;
		new AlertDialog.Builder(this)
				.setTitle(R.string.goods_category_delete_dialog_title)
				.setMessage(R.string.goods_supplier_delete_dialog_content)
				.setPositiveButton("确定", new DialogInterface.OnClickListener() {

					public void onClick(DialogInterface dialog, int which) {
						Suppliers supplier = suppliersItems.get(position);
						suppliersDao.delete(supplier.getId());
						suppliersItems.remove(position);
						listAdapter.notifyDataSetChanged();
					
						// 确定按钮事件
						// setResult(RESULT_OK);
						dialog.dismiss();
						// finish();
					}
				})
				.setNegativeButton("取消", new DialogInterface.OnClickListener() {

					public void onClick(DialogInterface dialog, int which) {
						// 取消按钮事件
						dialog.dismiss();
					}
				}).show();
	}
	
	
	private void initData() {
		//categoryItems = new ArrayList<Category>();
	//	DBHelper dbHelper = new DBHelper(getApplicationContext());
		//dbHelper.openDatabase();	
		suppliersItems = (ArrayList)suppliersDao.getAll();
		 
		
	}
	
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		switch (resultCode){  
        case RESULT_OK:  
          Bundle b = data.getExtras();  
          Suppliers supplier =  (Suppliers)b.getSerializable("supplier");
         // String string = b.getString("CALCULATION");        
          suppliersItems.add(0,supplier);
			if (listAdapter != null) {
				listAdapter.notifyDataSetChanged();
			}   
           
        }  

	}

	@Override
	public void onUpdate() {
		// TODO Auto-generated method stub
		
	}
	
}
